package com.ywd.materials.admin.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ywd.materials.admin.common.Common;
import com.ywd.materials.admin.dto.AdminRoleDto;
import com.ywd.materials.admin.dto.AdminRoleMenuDto;
import com.ywd.materials.admin.dto.query.AdminRoleQuery;
import com.ywd.materials.admin.entity.AdminRoleEntity;
import com.ywd.materials.admin.entity.AdminRoleMenuEntity;
import com.ywd.materials.admin.entity.AdminUserEntity;
import com.ywd.materials.admin.exception.CommonException;
import com.ywd.materials.admin.exception.RPage;
import com.ywd.materials.admin.mapper.AdminRoleMapper;
import com.ywd.materials.admin.vo.AdminMenuPartVo;
import com.ywd.materials.admin.vo.AdminMenuTreeVo;
import com.ywd.materials.admin.vo.AdminRoleDetailsVo;
import com.ywd.materials.admin.vo.AdminRoleVo;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * YWD
 * 2025/11/12
 */
@AllArgsConstructor
@Service
public class AdminRoleServiceImpl extends ServiceImpl<AdminRoleMapper, AdminRoleEntity> {

    private final AdminRoleMenuServiceImpl adminRoleMenuServiceImpl;
    private final AdminMenuServiceImpl adminMenuServiceImpl;

    public RPage<List<AdminRoleVo>> getRolePage(AdminRoleQuery query) {
        Page<Object> page = PageHelper.startPage(query.getPage(), query.getLimit());
        LambdaQueryWrapper<AdminRoleEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(AdminRoleEntity::getIsDeleted, 0)
                .like(ObjectUtil.isNotEmpty(query.getName()), AdminRoleEntity::getName, query.getName())
                .eq(ObjectUtil.isNotEmpty(query.getStatus()), AdminRoleEntity::getStatus, query.getStatus());
        List<AdminRoleEntity> list = list(queryWrapper);
        if (CollectionUtil.isEmpty(list)) return null;
        List<AdminRoleVo> list1 = BeanUtil.copyToList(list, AdminRoleVo.class);
        return RPage.ok(list1, query.getPage(), query.getLimit(), page.getTotal());
    }

    public AdminRoleDetailsVo getRoleDetails(Long id) {
        AdminRoleEntity roleEntity = getById(id);
        AdminRoleDetailsVo detailsVo = new AdminRoleDetailsVo();
        BeanUtil.copyProperties(roleEntity, detailsVo);
        List<AdminMenuTreeVo> list = adminMenuServiceImpl.getByRoleId(id);
        if (CollectionUtil.isNotEmpty(list)) {
            List<AdminMenuPartVo> list1 = BeanUtil.copyToList(list, AdminMenuPartVo.class);
            detailsVo.setMenuTreeVoList(list1);
        }
        return detailsVo;
    }

    public Boolean addRole(AdminRoleDto dto) {
        return save(BeanUtil.copyProperties(dto, AdminRoleEntity.class));
    }

    public Boolean updateRole(AdminRoleDto dto) {
        return updateById(BeanUtil.copyProperties(dto, AdminRoleEntity.class));
    }

    public Boolean deleteRole(Long id) {
        List<AdminUserEntity> list = this.baseMapper.getRoleId(id);
        if (CollectionUtil.isNotEmpty(list)) throw new CommonException("角色正在被使用,不能删除");
        adminRoleMenuServiceImpl.removeByRoleId(id);
        return removeById(id);
    }

    public List<Common> getRolePartList(String name) {
        LambdaQueryWrapper<AdminRoleEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(AdminRoleEntity::getIsDeleted, 0)
                .like(ObjectUtil.isNotEmpty(name), AdminRoleEntity::getName, name);
        List<AdminRoleEntity> list = list(queryWrapper);
        if (CollectionUtil.isEmpty(list)) return null;
        return BeanUtil.copyToList(list, Common.class);
    }

    public Boolean addRoleMenu(AdminRoleMenuDto dto) {
        //删除再新增
        adminRoleMenuServiceImpl.removeByRoleId(dto.getId());
        return adminRoleMenuServiceImpl.addRoleMenu(dto);
    }


}
